package gui;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;

/**
 * Trida predstavujici jednu zalozku v GUI
 */
public class Tab {
	
	private String conversationName;
	private Document doc;
	private Map<String, ColorScheme> contacts;
	private DefaultTableModel model;
	
	public Tab(String conversationName, Document doc){
		this.conversationName = conversationName;
		this.doc = doc;
		this.contacts = new HashMap<String, ColorScheme>();
		String[] column = {"Kontakty"};
	    this.model = new DefaultTableModel(column, 0){

			private static final long serialVersionUID = 1L;

			@Override
	        public boolean isCellEditable(int row, int column) {
	           //all cells false
	           return false;
	        }
	    };
	    
		System.out.println("Konverzace: " + conversationName);
	}
	
	/**
	 * Operace se jmenem konverzace
	 */
	public String getConversationName(){
		return conversationName;
	}

	public void setConversationName(String conversationName){
		this.conversationName = conversationName;
	}

	/**
	 * Operace s dokumentem
	 */
	public Document getDoc(){
		return doc;
	}

	public void setDoc(Document doc){
		this.doc = doc;
	}

	/**
	 * Operace s mapou
	 */
	// Vlozi jeden zaznam do mapy
	public void addContact(String contact){
		contacts.put(contact, new ColorScheme());
	}
	
	// Vlozi mapu do mapy
	public void addAllContacts(Map<String, ColorScheme> contacts){
		this.contacts.putAll(contacts);
	}
	
	// Odstrani pozadovany zaznam z mapy
	public void removeContact(String contact){
		if(contacts.containsKey(contact)){
			contacts.remove(contact);
		}
	}
	
	// Vrati mnozinu klicu z mapy
	public Set<String> getContacts(){
		return contacts.keySet();
	}
	
	// Vrati velikost mapy
	public int getContactsSize(){
		return contacts.size();
	}
	
	// Vrati barevne schema pro dany kontakt - cas
	public SimpleAttributeSet getTimeStyle(String name){
		return contacts.get(name).getTimeStyle();
	}
	
	// Vrati barevne schema pro dany kontakt - jmeno
	public SimpleAttributeSet getNameStyle(String name){
		return contacts.get(name).getNameStyle();
	}
	
	// Vrati barevne schema pro dany kontakt - text zpravy
	public SimpleAttributeSet getNormalStyle(String name){
		return contacts.get(name).getNormalStyle();
	}
	
	/**
	 * Operace s tabulkou kontaktu
	 */
	public DefaultTableModel getModel(){
		return model;
	}

	// Vlozi kontakt do tabulky
	public void putToModel(String contact){
		model.addRow(new Object[]{contact});
		model.fireTableDataChanged();
	}
	
	// Vlozi kontakty z pole do tabulky
	public void putAllToModel(Set<String> contacts){
		for(String contact : contacts){
			model.addRow(new Object[]{contact});
		}
		model.fireTableDataChanged();
	}
	
	// Vrati kontakt z tabulky na pozadovane radce
	public String getFromModel(int row){
		return model.getValueAt(row, 0).toString();	
	}
	
	// Odstrani kontakt z tabulky
	public void removeFromModel(String contact){		
		for(int i = 0; i <model.getRowCount();i++){
			if(model.getValueAt(i, 0).equals(contact)){
				model.removeRow(i);
				break;
			}
		}	
		model.fireTableDataChanged();
	}
}
